(function () {

    var x;

    function onInit() {

        onInitData();
        onGetUserInfoShow();
        onEditUserInfo();
    }

    function onInitData() {
        onDragTab();
        onTouchMoveUpdateHeaderBg();
    }

    /**
     * 获取用户信息
     */
    function onGetUserInfoShow() {
        var userId = backService.localLoginInfo.id;
        backService.user.onGetUserInfoShow(userId, function (json) {
//			console.log(json.data)
            var data = json.data;
            onShowUserInfo(data);
        }, function (e) {
            app.showToast(e.msg)
        });
    }

    /**
     * 显示用户信息
     * @param {Object} data
     */
    function onShowUserInfo(data) {
        var avatar = data.avatar;
        var nickname = data.nickName;
        var info = data.info;
        var friendNum = data.friendNum;
        var releaseNum = data.releaseNum;
        var sex = data.sex;
        if (avatar) {
            var src = backService.getFileUrl(data.avatar);
            x.$view.find('.avatar_img').css("background-image", "url(" + src + ")");
        } else {
            x.$view.find('.avatar_img').css("background-image", "url(img/message/index/head.jpg)");
        }

        x.$view.find('.fil_user').addClass(sex == 1 ? 'men' : "women");
        x.$view.find('.user_name').text(nickname != null ? nickname : "您还没有设置昵称");
        x.$view.find('.fil-desc').text(info != null ? info : "您还没有编辑简介!");
        x.$view.find('.fri span').text(friendNum);
        x.$view.find('.word span').text(releaseNum);
    }

    /**
     * 进入编辑用户信息页面
     */
    function onEditUserInfo() {
        x.$view.find('.m-text-cut').on("click", function () {
            app.gotoPage("/user/edit.html", "slide:left");
        });
    }

    /**
     * 拖动/切换页面
     */
    function onDragTab() {
        var left = x.$view.find('.nav_item .index').get(0).offsetLeft;
        x.$view.find('.nav_item .bar').css("left", left + 10);
        x.$view.find('.show_cont').get(0).addEventListener("drag", function (e) {
            var deltaX = e.detail.deltaX;
            var width = x.$view.find('.show_cont').width();
            var left = x.$view.find('.show_cont').scrollLeft();
            if (deltaX < 100 && deltaX > 0) {// 右滑动
                if (left < width / 2) {
                    x.view.element.data('disableSwipeBackward', false);
                }
            }
        }, false);
        x.$view.find('.show_cont').get(0).addEventListener("dragend", function (e) {
            var deltaX = e.detail.deltaX;
            var width = x.$view.find('.show_cont').width();
            var left = x.$view.find('.show_cont').scrollLeft();
            if (deltaX > 100) {
                x.$view.find('.show_cont').animate({scrollLeft: 0}, 300);
                x.$view.find(".nav_item .index").addClass("m_cur").siblings("li").removeClass("m_cur");
            } else if (deltaX < 100 && deltaX > 0) {// 右滑动
                if (left > width / 2) {
                    x.$view.find('.show_cont').animate({scrollLeft: x.$view.find('.show_cont').width()}, 300);
                }
            } else if (deltaX < -100) {
                x.$view.find('.show_cont').animate({scrollLeft: x.$view.find('.show_cont').width()}, 300);
                x.$view.find(".nav_item .work").addClass("m_cur").siblings("li").removeClass("m_cur");
            } else if (deltaX < 0 && deltaX >= -100) {// 左滑动
                if (left < width / 2) {
                    x.$view.find('.show_cont').animate({scrollLeft: 0}, 300);
                }
            }

            // console.log("left:" + left + ", width:" + width + ", deltaX:" + deltaX);
        }, false);

        // 点击tab切换内容
        x.$view.find('.nav_item').on("click", 'li', function () {
            var that = $(this);
            if (that.hasClass("m_cur")) {
                return;
            }
            that.addClass("m_cur").siblings("li").removeClass("m_cur");
            if (that.hasClass("index")) {
                x.$view.find('.show_cont').animate({scrollLeft: 0}, 300);
            } else {
                x.$view.find('.show_cont').animate({scrollLeft: x.$view.find('.show_cont').width()}, 300);
            }
        });
    }

    function onTouchMoveUpdateHeaderBg() {
        document.addEventListener("touchmove", function () {
            var scrollTop = document.documentElement.scrollTop === 0 ? $("#user_show").find(".km-content").get(0).scrollTop : document.documentElement.scrollTop;
            var setScrollTop = scrollTop / 100;
            if (setScrollTop > 1) {
                setScrollTop = 1;
            }
            x.$view.find('#header').css("background-color", "rgba(251, 251, 251, " + setScrollTop + ")");
            if (scrollTop >= 136) {
                x.$view.find('.sub').addClass("fixed");
                x.$view.find('.show_cont').addClass("fixed");
            }
        }, false);
    }

    window.onViewInit_user_show = function (view) {
        x = view.context;
        onInit();
        view.element.data('disableSwipeBackward', true);
    }

})();
